import 'jquery-ui-dist/jquery-ui'
import Handsontable from 'handsontable';
const FiledtreeDrag = {

    addDrag: function () {

        setTimeout(function () {



            $("#dataTree ul .ivu-tree-children li").draggable({ appendTo: "#workexelce", revert: "invalid", helper: 'clone' });

            $("#workexelce").droppable({
                revert: true,
                drop: function (event, ui) {
                    let hot = window.exelceContext.hot;

                    ui.helper.hide();
                    let isList = ui.draggable.find('.dataset-field').attr('islist')
                    let dbCode = ui.draggable.find('.dataset-field').attr('dbCode')
                    let text = ui.draggable.text().trim()
                    let excelExpressChar = isList ? '#' : '$';
                    let excelText = `${excelExpressChar}{${dbCode}.${text}}`;

                    let $destination = $(document.elementFromPoint(event.clientX, event.clientY));

                    let $tr = $destination.closest('tr');
                    let $tbody = $tr.closest('tbody');

                    let col = $tr.children().index($destination) - 1;

                    let row = $tbody.children().index($tr);

                    let cellDef = window.exelceContext.getCell(row, col);

                    if (cellDef && cellDef.value.type !== 'dataset') {
                        window.exelceContext.removeCell(cellDef);
                    }
                    let width = window.exelceContext.buildWidth(col,0);
                    
                    let height = window.exelceContext.buildHeight(row,0);
                    cellDef = {
                        value: { type: 'dataset', dbCode: dbCode, fieldName: text, order:'none',exps: excelText,width:width,height:height},
                        x: col,
                        y: row,
                        cellStyle: {}
                    };
                    window.exelceContext.addCell(cellDef);
                    hot.setDataAtCell(row, col, excelText);
                    hot.render();
                    Handsontable.hooks.run(hot, 'afterSelectionEnd', row, col, row, col);
                },
                over: function (event, elem) {
                    $(this).addClass("over");
                },
                out: function (event, elem) {
                    $(this).removeClass("over");
                }
            });

        }, 300);
    }
}
export default FiledtreeDrag

